package com.java502.shirodemo502.realm;

import com.java502.shirodemo502.pojo.Access;
import com.java502.shirodemo502.pojo.User;
import com.java502.shirodemo502.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author:Teacher黄
 * @date:Created at 2019/12/30
 */
@Component
public class MyRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;

    /**
     * 授权的方法
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        // 去数据库(Redis)
        // 拿到用户名
        String  userName = (String) principalCollection.getPrimaryPrincipal();
        // 获取用户对象
        User user = getUserByUserName(userName);
        // 获取权限信息
        Set<String> permissions = getPermissionByUser(user);
        // 获取角色数据
        Set<String> roles = getRoleByUser(user);
        // 返回授权信息
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.setStringPermissions(permissions);
        authorizationInfo.setRoles(roles);
        return authorizationInfo;
    }

    /**
     * 获取角色数据
     * @param user
     * @return
     */
    private Set<String> getRoleByUser(User user) {
        Set<String> roles = new HashSet<>();
        roles.add(user.getRole().getRoleName());
        return roles;
    }

    /**
     * 获取权限数据
     * @param user
     * @return
     */
    private Set<String> getPermissionByUser(User user) {
        Set<String> permissions = new HashSet<>();
        // 获取权限列表
        List<Access> accessList = user.getRole().getAccessList();
        for (Access access : accessList) {
            permissions.add(access.getAccessUrl());
        }
        return permissions;
    }

    /**
     * 获取用户对象
     * @param userName
     * @return
     */
    private User getUserByUserName(String userName) {
        if(userName == null || userName.isEmpty()){
            return null;
        }

        return userService.getUser(userName);
    }

    /**
     * 认证的方法(登陆)
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        // 通过token去获取用户名
        String userName = (String) authenticationToken.getPrincipal();
        // 通过查询数据库得到对应的用户信息
        String pwd = getPwdByUserName(userName);
        if (pwd == null) {
            return null;
        }
        // 认证信息
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(userName, pwd, "myRealm");
        // 设置加盐
        info.setCredentialsSalt(ByteSource.Util.bytes("java502"));

        return info;
    }

    /**
     * 通过用户名查询用户密码
     * @param userName
     * @return
     */
    private String getPwdByUserName(String userName) {
        if(userName == null || userName.isEmpty()){
            return null;
        }

        User user = userService.getUser(userName);

        if(user == null){
            return null;
        }

        return user.getUserPwd();
    }
}
